package com.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description: MessageController
 * @author: Administrator
 * @create: 2018-08-17 10:49
 **/
@Controller
@RequestMapping("/send")
public class MessageController {
    //websocket服务层调用类
    @Autowired
    private WSMessageService wsMessageService;

    /**
     * 给对应用户发送消息
     * @param userId
     * @param message
     * @return
     */
    @RequestMapping(value="/test")
    @ResponseBody
    public String TestWS(@RequestParam(value="userId",required=true) String userId,
                         @RequestParam(value="message",required=true) String message){
        System.out.println("收到发送请求，向用户{"+userId+"}的消息：{"+message+"}");

        if(wsMessageService.sendToAllTerminal(userId, message)){
            return "发送成功";
        }else{
            return "发送失败";
        }
    }

    /**
     * 给所有用户发送消息
     * @param message
     * @return
     */
    @RequestMapping(value = "/sendmsg")
    @ResponseBody
    public  String sendmsg(String message){
        if(wsMessageService.sendmsgToAll( message)){
            return "发送成功";
        }else{
            return "发送失败";
        }
    }

}
